/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package as.internal;

import as.Jwintab;
import as.pointer.PointerEventListener;
import java.awt.Dimension;
import net.idgames.engine.id2.input.PointerEvent;

/**
 *
 * @author David
 */
public class TabletProcessor
		implements Runnable {

	private TabletStatus status;
	private PointerEventListener listener;
	private int previousPressure = 0;
	private Dimension area;

	public TabletProcessor(PointerEventListener listener, Dimension area) {
		this.listener = listener;
		this.area = area;
	}

	void delayFunc(long t) {
		try {
			Thread.sleep(t);
		} catch (Exception e) {}
	}

	PointerEvent makeEvent() {
		return PointerEvent.newFromTablet(status.scaleX(area.width), status.invertScaleY(area.height), status.button, status.pressure);
	}

	public void run() {
		status = new TabletStatus();
		while (true) {
			if (Jwintab.getPacket(status)) {
				// Handle the packet.
				if (status.pressure == 0) {
					if (previousPressure > 0) {
						listener.onPointerRelease(makeEvent());
					} else {
						listener.onPointerMove(makeEvent());
					}
				} else {
					if (previousPressure == 0) {
						listener.onPointerPress(makeEvent());
					} else {
						listener.onPointerDrag(makeEvent());
					}
				}
				previousPressure = status.pressure;
			}
			delayFunc(1);
		}
	}

}
